const Mongoose  = require('mongoose')

// 定义一个模型中的字段
const schema = new Mongoose.Schema({
    name: { type: String },
    parent: { type: Mongoose.Schema.Types.ObjectId, ref: 'Category' },
})

schema.virtual('children', {
  localField: '_id',
  foreignField: 'parent',
  justOne: false,
  ref: 'Category'
})

schema.virtual('newsList', {
  localField: '_id',
  foreignField: 'categories',
  justOne: false,
  ref: 'Article'
})

// 导出数据库模型 根据schema定义的方式创建模型 相当于SQL中的表
module.exports = Mongoose.model('Category', schema)